package com.goblin.model.uaa.po;

import java.util.List;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.goblin.model.base.BaseEntity;
import lombok.Data;

@Data
@Table(name = "t_test_uaa_priv")
@Entity
public class Priv extends BaseEntity {

	private static final long serialVersionUID = -3707035119141881581L;

	@Column(name = "label", nullable = false, columnDefinition = " varchar(255) COMMENT '权限名称'")
	private String label;

	@Column(name = "code", nullable = false, columnDefinition = " varchar(255) COMMENT '权限编码'")
	private String code;

	@Column(name = "url", nullable = false, columnDefinition = " varchar(255) COMMENT '请求路径'")
	private String url;

	@Column(name = "method", nullable = false, columnDefinition = " varchar(255) COMMENT '请求方法'")
	private String method;

	@Column(name = "detail", nullable = false, columnDefinition = " varchar(255) COMMENT '权限功能描述'")
	private String detail;

	@Column(name = "is_root", nullable = false, columnDefinition = " tinyint(1) COMMENT '是否为根节点'")
	private Boolean isRoot;

	@Column(name = "parent_id", nullable = false, columnDefinition = " int(11) COMMENT '父权限'")
	private Integer parentId;

	//all: 所有情况下均进行关联操作，即save-update和delete。
	//FetchType.EAGER：急加载，加载一个实体时，定义急加载的属性会立即从数据库中加载
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_test_priv_sub", joinColumns = @JoinColumn(name = "priv_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "subpriv_id", referencedColumnName = "id"), foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
	private List<Priv> children;

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		if (!super.equals(o))
			return false;
		Priv priv = (Priv) o;
		return Objects.equals(code, priv.code);
	}

	@Override
	public int hashCode() {
		return Objects.hash(super.hashCode(), code);
	}
}

